package com.mhkj.base.itemInfo.group.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.itemInfo.group.entity.Group;
import com.mhkj.base.itemInfo.group.service.GroupService;
import com.mhkj.base.itemInfo.item.entity.Item;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.base.region.entity.TSysNation;
import com.mhkj.base.region.service.NationService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 组团
 */

@Controller
@RequestMapping("/group")
public class GroupController extends BaseController<Group> {
	@Autowired
	private GroupService groupService;
	@Autowired
	private NationService nationService;
	@Autowired
	private ItemService itemService;
	@Autowired
	private OrgService orgService;
	
	/**
	 * 分页查询组团
	 * 
	 */
	@RequestMapping(value="/listByPage", method=RequestMethod.POST)
	@ResponseBody
	public Pagination<Group> listByPage( Group Group, int pageIndex, int pageSize ) {
		Pagination<Group> pagination = groupService.listByPage(Group,pageIndex, pageSize);
		return pagination;
	}

	/**
	 * 保存
	 * 
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(Group Group) {
		
		Long cityId = Group.getCityId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "城市为空!");
		}
		TSysNation nation = nationService.getById(cityId);
		Group.settSysNation(nation);
		Long itemId = Group.getItemId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "项目为空!");
		}
		Item item = itemService.getById(itemId);
		Group.setItem(item);
		String message = groupService.save(Group);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(Group Group) {
		Long cityId = Group.getCityId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "城市为空!");
		}
		TSysNation nation = nationService.getById(cityId);
		Group.settSysNation(nation);
		Long itemId = Group.getItemId();
		if(StringUtils.isEmpty(cityId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "项目为空!");
		}
		Item item = itemService.getById(itemId);
		Group.setItem(item);
		String message = groupService.edit(Group);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del",method={RequestMethod.POST})
	@ResponseBody
	public ResponseMessage del(Long id) {
		if(StringUtils.isEmpty(id)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "id为空!");
		}
		Group Group = new Group();
		Group.setId(id);
		String message = groupService.del(Group);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

 
	/**
	 * 查询组团信息
	 */
	@RequestMapping(value="/info", method=RequestMethod.POST)
	@ResponseBody
	public Group info(@RequestParam(value = "id", required = true) long id) {
		Group Group = groupService.getById(id);
		return Group;
	}


	/**
	 * 批量删除组团
	 */
	@RequestMapping(value="/delAll", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delAll(@RequestParam(value="id",required=true) String id) {
		String[] split = id.split(",");
		for (String str : split) {
			long parseLong = Long.parseLong(str);
			Group Group = groupService.getById(parseLong);
			groupService.del(Group);
		}
		
		return ResponseMessage.createMessage(true, "ok");
	}
 
	/**
	 * 获取项目下的组团
	 */
	@RequestMapping(value="/getGroupByItemId", method=RequestMethod.POST)
	@ResponseBody
	public List<Group> getGroupByItemId(@RequestParam(value="itemId",required=true) Long itemId) {
		
		List<Group> groupList = groupService.getGroupByItemId(itemId);
		 
		return groupList;
	}
	
 
	
	
	
	
}
